package com.hemerasolutions.v2trainer.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
 


import javax.faces.context.FacesContext;
import javax.servlet.ServletRequest;

import java.util.HashMap;
 
@SuppressWarnings("rawtypes")
@Component(value = "msg")
@Scope("request")
public class SpringMessageBean extends HashMap {
 
	private static final long serialVersionUID = 1L;

	@Autowired
    private MessageSource messageSource;
 
    @Override
    public String get(Object key) {
        ServletRequest request = (ServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String message;
        try {
            message = messageSource.getMessage((String) key, null, request.getLocale());
        }
        catch (NoSuchMessageException e) {
            message = "???" + key + "???";
        }
        return message;
    }
}